﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using YiSha.Util;
using YiSha.Util.Model;
using YiSha.Entity;
using YiSha.Model;
using YiSha.Admin.Web.Controllers;
using YiSha.Entity.YiDongYuan;
using YiSha.Business.YiDongYuan;
using YiSha.Model.Param.YiDongYuan;
using YiSha.Web.Code;
using System.Drawing;
using System.IO;
using Microsoft.AspNetCore.Http;
using System.Text;

namespace YiSha.Admin.Web.Areas.YiDongYuan.Controllers
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2021-06-07 10:00
    /// 描 述：非道路移动机械控制器类
    /// </summary>
    [Area("YiDongYuan")]
    public class MechanicsController : BaseController
    {
        private MechanicsBLL mechanicsBLL = new MechanicsBLL();

        #region 视图功能

        [AuthorizeFilter("yidongyuan:mechanics:view")]
        public ActionResult MechanicsIndex()
        {
            return View();
        }

        public ActionResult MechanicsForm()
        {
            return View();
        }

        //非道机械设备信息卡
        public ActionResult InfoCard(long id)
        {
            MechanicsEntity mechanics = mechanicsBLL.GetEntity(id).Result.Data;
            ViewBag.Mechanics = mechanics;
            return View();
        }

        #endregion 视图功能

        #region 获取数据

        [HttpGet]
        [AuthorizeFilter("yidongyuan:mechanics:search")]
        public async Task<ActionResult> GetListJson(MechanicsApplyParam param)
        {
            TData<List<MechanicsEntity>> obj = await mechanicsBLL.GetList(param);
            return Json(obj);
        }

        [HttpGet]
        [AuthorizeFilter("yidongyuan:mechanics:search")]
        public async Task<ActionResult> GetPageListJson(MechanicsApplyParam param, Pagination pagination)
        {
            TData<List<MechanicsEntity>> obj = await mechanicsBLL.GetPageList(param, pagination);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJson(long id)
        {
            TData<MechanicsEntity> obj = await mechanicsBLL.GetEntity(id);
            return Json(obj);
        }

        [HttpGet]
        public async Task<ActionResult> GetFormJsonByCode(string code)
        {
            TData<MechanicsEntity> obj = await mechanicsBLL.GetEntity(code);
            return Json(obj);
        }

        #endregion 获取数据

        #region 提交数据

        [HttpPost]
        [AuthorizeFilter("yidongyuan:mechanics:add,yidongyuan:mechanics:edit")]
        public async Task<ActionResult> SaveFormJson(MechanicsEntity entity)
        {
            TData<string> obj = await mechanicsBLL.SaveForm(entity);
            return Json(obj);
        }

        [HttpPost]
        [AuthorizeFilter("yidongyuan:mechanics:delete")]
        public async Task<ActionResult> DeleteFormJson(string ids)
        {
            TData obj = await mechanicsBLL.DeleteForm(ids);
            return Json(obj);
        }

        #endregion 提交数据

        /// <summary>
        /// 下载二维码
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public FileResult QrCode(string id)
        {
            string url = GetAbsoluteUri(Request);
            url = url.Replace(@"YiDongYuan/", "");
            Image img = QRcodeHelper.CreateQRcodeImage(url);
            byte[] bt = null;
            if (!img.Equals(null))
            {
                using (MemoryStream mostream = new MemoryStream())
                {
                    Bitmap bmp = new Bitmap(img);
                    bmp.Save(mostream, System.Drawing.Imaging.ImageFormat.Png);//将图像以指定的格式存入缓存内存流

                    bt = new byte[mostream.Length];
                    mostream.Position = 0;//设置留的初始位置
                    mostream.Read(bt, 0, Convert.ToInt32(bt.Length));
                }
            }
            return File(bt, "image/png", id + ".png");
        }

        public string GetAbsoluteUri(HttpRequest request)
        {
            return new StringBuilder()
             .Append(request.Scheme)
             .Append("://")
             .Append(request.Host)
             .Append(request.PathBase)
             .Append(request.Path)
             .Append(request.QueryString)
             .ToString();
        }
    }
}
